<template>
  <div>
    <div>
      <el-breadcrumb separator="/" style="padding-bottom: 10px;">
        <el-breadcrumb-item>我的工作台</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/item/list' }">商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品修改</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :model="itemForm" :rules="rules" ref="itemForm" label-width="100px" class="demo-itemForm"
             style="margin: 30px 300px;width: 700px;">
      <el-form-item label="商品名称" prop="name" style="width: 300px;">
        <el-input v-model="itemForm.name"></el-input>
      </el-form-item>
      <el-form-item label="条码" prop="barcode" style="width: 300px;">
        <el-input v-model="itemForm.barcode"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price" style="width: 300px;">
        <el-input v-model="itemForm.price"></el-input>
      </el-form-item>
      <el-form-item label="商品类别" prop="type" style="width: 300px;">
        <el-select v-model="itemForm.type" placeholder="商品类别">
          <el-option label="计件商品" value=0></el-option>
          <el-option label="计重商品" value=1></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单位" prop="unit" style="width: 300px;">
        <el-input v-model="itemForm.unit"></el-input>
      </el-form-item>
      <el-form-item style="width: 300px;">
        <el-button type="primary" @click="onSubmit('itemForm')">更新</el-button>
        <el-button @click="resetForm()">还原</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import itemService from '../global/service/itemService';

export default {
  data() {
    const validatePrice = (rule, value, callback) => {
      if (value === undefined || value === '') {
        callback(new Error('请输入价格'));
      }
      let regPos = /^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*))$/;
      if (!regPos.test(value)) {
        callback(new Error('必须为数值'))
      }
      if (parseFloat(value) > 10000 || parseFloat(value) < 1) {
        callback(new Error('金额在 1 - 10000'))
      } else {
        callback();
      }
    };
    const validateUnit = (rule, value, callback) => {
      if (value.length > 5) {
        callback(new Error('长度不超过 5 个字符 '));
      } else {
        callback();
      }
    };
    return {
      id: '',
      itemForm: {
        barcode: '',
        name: '',
        price: '',
        type: '',
        unit: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入商品名称', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 20 个字符', trigger: 'blur'},
        ],
        barcode: [
          {required: true, message: '请输入商品条码', trigger: 'blur'},
          {min: 5, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'},
        ],
        price: [
          {required: true, validator: validatePrice, trigger: 'blur'},
        ],
        type: [
          {required: true, message: '请选择商品类型', trigger: 'change'},
        ],
        unit: [
          {max: 5, message: '长度不超过 5 个字符', trigger: 'blur'},
        ],
      },
    }
  },
  created() {
    this.id = this.$route.params.id
    this.getItem(this);
  },
  methods: {
    getItem() {
      itemService.get(this.id)
        .then((item) => {
          this.itemForm = item
        })
    },
    onSubmit(itemForm) {
      this.$refs[itemForm].validate((valid) => {
        if (valid) {
          itemService.update(this.id, this.itemForm)
            .then(() => {
              this.$message.success('更新成功！')
              this.$router.push({
                name: 'ItemList',
              })
            })
        }
      });
    },
    resetForm() {
      this.getItem();
    },
  },
}
</script>

<style scoped>

</style>
